﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace ExcelPlugin {
    public static class MsgHelper {
        static Form frm = new Form();
        public static string ResultValue;
        static DialogResult dialogResult;
        public static DialogResult ShowInputBoxForm(string content, string title_text, int count, string[] label_name, ref string[] value) {
            frm.Controls.Clear();
            ResultValue = "";

            frm.MinimizeBox = false;
            frm.MaximizeBox = false;
            frm.FormBorderStyle = FormBorderStyle.FixedDialog;
            frm.Size = new Size(350, 110 + (20 * count));
            frm.Text = title_text;
            frm.ShowIcon = false;
            frm.ShowInTaskbar = false;
            frm.StartPosition = FormStartPosition.CenterParent;

            //panel
            Panel panel = new Panel();
            panel.Location = new Point(0, 0);
            panel.Size = new Size(340, 40 + (20 * count));
            panel.BackColor = Color.White;
            frm.Controls.Add(panel);

            foreach(Button btn in Btns(count * 20)) {
                frm.Controls.Add(btn);
            }
            Control[] ctls = AddTextBoxControl(panel, count, label_name);
            foreach(var ctl in ctls) {
                panel.Controls.Add(ctl);
            }
            frm.ShowDialog();
            if(dialogResult == DialogResult.OK) {
                value[0] = ctls[0].Text;
                value[1] = ctls[1].Text;
                value[2] = ctls[2].Text;
            }
            return dialogResult;
        }

        static Button[] Btns(int offset) {
            Button[] returnBtns = new Button[2];

            Button okButton = new Button();
            Button cancelButton = new Button();
            okButton.Text = "OK";
            okButton.Name = "OK";
            cancelButton.Text = "Cancel";
            cancelButton.Name= "Cancel";
            okButton.Location = new Point(170, 40 + offset);
            returnBtns[0] = okButton;
            cancelButton.Location = new Point(250, 40 + offset);
            returnBtns[1] = cancelButton;

            foreach(Button btn in returnBtns) {
                btn.Size = new Size(75, 23);
                btn.Click += (object sender, EventArgs e) =>
                {
                    Button btnSender = sender as Button;
                    switch(btnSender.Name) {
                        case "Cancel":
                            dialogResult = DialogResult.Cancel;
                            break;
                        default:
                            dialogResult = DialogResult.OK;
                            break;
                    }
                    frm.Close();
                };
            }

            return returnBtns;
        }

        static Control[] AddTextBoxControl(Panel panel, int count, string[] label_name) {
            Control[] res = new Control[count];
            for(int i = 0; i < res.Length; i++) {
                int yPos = 20 + (20 * i);
                Label label = new Label();
                label.Text = label_name[i];
                label.Size = new Size(80, 23);
                label.Location = new Point(20, yPos);
                label.TextAlign = ContentAlignment.MiddleLeft;
                panel.Controls.Add(label);

                TextBox tb = new TextBox();
                tb.Size = new Size(180, 23);
                tb.Location = new Point(110, yPos);
                res[i] = tb;
            }
            return res;
        }
        public static void PopErrorMsg(string content, params object[] str_val ) {
            MessageBox.Show(string.Format(content, str_val), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public static void PopInformationMsg(string content, params object[] str_val) {
            MessageBox.Show(string.Format(content, str_val), "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static void PopQueryMsg(string content, Action okAction) {
            DialogResult res = MessageBox.Show(content, "Query", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if(res == DialogResult.OK && okAction != null) {
                okAction();
            }
        }
    }

}
